home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / mimify.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  12KB  |  498 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Mimification and unmimification of mail messages.
  5.  
  6. Decode quoted-printable parts of a mail message or encode using
  7. quoted-printable.
  8.  
  9. Usage:
  10.         mimify(input, output)
  11.         unmimify(input, output, decode_base64 = 0)
  12. to encode and decode respectively.  Input and output may be the name
  13. of a file or an open file object.  Only a readline() method is used
  14. on the input file, only a write() method is used on the output file.
  15. When using file names, the input and output file names may be the
  16. same.
  17.  
  18. Interactive usage:
  19.         mimify.py -e [infile [outfile]]
  20.         mimify.py -d [infile [outfile]]
  21. to encode and decode respectively.  Infile defaults to standard
  22. input and outfile to standard output.
  23. '''
  24. MAXLEN = 200
  25. CHARSET = 'ISO-8859-1'
  26. QUOTE = '> '
  27. import re
  28. import warnings
  29. warnings.warn('the mimify module is deprecated; use the email package instead', DeprecationWarning, 2)
  30. __all__ = [
  31.     'mimify',
  32.     'unmimify',
  33.     'mime_encode_header',
  34.     'mime_decode_header']
  35. qp = re.compile('^content-transfer-encoding:\\s*quoted-printable', re.I)
  36. base64_re = re.compile('^content-transfer-encoding:\\s*base64', re.I)
  37. mp = re.compile('^content-type:.*multipart/.*boundary="?([^;"\n]*)', re.I | re.S)
  38. chrset = re.compile('^(content-type:.*charset=")(us-ascii|iso-8859-[0-9]+)(".*)', re.I | re.S)
  39. he = re.compile('^-*\n')
  40. mime_code = re.compile('=([0-9a-f][0-9a-f])', re.I)
  41. mime_head = re.compile('=\\?iso-8859-1\\?q\\?([^? \t\n]+)\\?=', re.I)
  42. repl = re.compile('^subject:\\s+re: ', re.I)
  43.  
  44. class File:
  45.     '''A simple fake file object that knows about limited read-ahead and
  46.     boundaries.  The only supported method is readline().'''
  47.     
  48.     def __init__(self, file, boundary):
  49.         self.file = file
  50.         self.boundary = boundary
  51.         self.peek = None
  52.  
  53.     
  54.     def readline(self):
  55.         if self.peek is not None:
  56.             return ''
  57.         line = self.file.readline()
  58.         if not line:
  59.             return line
  60.         return line
  61.  
  62.  
  63.  
  64. class HeaderFile:
  65.     
  66.     def __init__(self, file):
  67.         self.file = file
  68.         self.peek = None
  69.  
  70.     
  71.     def readline(self):
  72.         if self.peek is not None:
  73.             line = self.peek
  74.             self.peek = None
  75.         else:
  76.             line = self.file.readline()
  77.         if not line:
  78.             return line
  79.         if he.match(line):
  80.             return line
  81.         while None:
  82.             self.peek = self.file.readline()
  83.             if (len(self.peek) == 0 or self.peek[0] != ' ') and self.peek[0] != '\t':
  84.                 return line
  85.             line = line + self.peek
  86.             self.peek = None
  87.             continue
  88.             return None
  89.  
  90.  
  91.  
  92. def mime_decode(line):
  93.     '''Decode a single line of quoted-printable text to 8bit.'''
  94.     newline = ''
  95.     pos = 0
  96.     while None:
  97.         res = mime_code.search(line, pos)
  98.         if res is None:
  99.             break
  100.         
  101.         newline = newline + line[pos:res.start(0)] + chr(int(res.group(1), 16))
  102.         pos = res.end(0)
  103.         continue
  104.         return newline + line[pos:]
  105.  
  106.  
  107. def mime_decode_header(line):
  108.     '''Decode a header line to 8bit.'''
  109.     newline = ''
  110.     pos = 0
  111.     while None:
  112.         res = mime_head.search(line, pos)
  113.         if res is None:
  114.             break
  115.         
  116.         match = res.group(1)
  117.         match = ' '.join(match.split('_'))
  118.         newline = newline + line[pos:res.start(0)] + mime_decode(match)
  119.         pos = res.end(0)
  120.         continue
  121.         return newline + line[pos:]
  122.  
  123.  
  124. def unmimify_part(ifile, ofile, decode_base64 = 0):
  125.     '''Convert a quoted-printable part of a MIME mail message to 8bit.'''
  126.     multipart = None
  127.     quoted_printable = 0
  128.     is_base64 = 0
  129.     is_repl = 0
  130.     if ifile.boundary and ifile.boundary[:2] == QUOTE:
  131.         prefix = QUOTE
  132.     else:
  133.         prefix = ''
  134.     hfile = HeaderFile(ifile)
  135.     while None:
  136.         line = hfile.readline()
  137.         if not line:
  138.             return None
  139.         if prefix and line[:len(prefix)] == prefix:
  140.             line = line[len(prefix):]
  141.             pref = prefix
  142.         else:
  143.             pref = ''
  144.         line = mime_decode_header(line)
  145.         if qp.match(line):
  146.             quoted_printable = 1
  147.             continue
  148.         
  149.         if decode_base64 and base64_re.match(line):
  150.             is_base64 = 1
  151.             continue
  152.         
  153.         if not prefix and repl.match(line):
  154.             is_repl = 1
  155.         
  156.         mp_res = mp.match(line)
  157.         if mp_res:
  158.             multipart = '--' + mp_res.group(1)
  159.         
  160.         if he.match(line):
  161.             break
  162.             continue
  163.         continue
  164.         if is_repl:
  165.             if quoted_printable or multipart:
  166.                 is_repl = 0
  167.             
  168.     while None:
  169.         line = ifile.readline()
  170.         if not line:
  171.             return None
  172.         line = re.sub(mime_head, '\\1', line)
  173.         if prefix and line[:len(prefix)] == prefix:
  174.             line = line[len(prefix):]
  175.             pref = prefix
  176.         else:
  177.             pref = ''
  178.         while multipart:
  179.             if line == multipart + '--\n':
  180.                 ofile.write(pref + line)
  181.                 multipart = None
  182.                 line = None
  183.                 break
  184.             
  185.             if line == multipart + '\n':
  186.                 ofile.write(pref + line)
  187.                 nifile = File(ifile, multipart)
  188.                 unmimify_part(nifile, ofile, decode_base64)
  189.                 line = nifile.peek
  190.                 if not line:
  191.                     break
  192.                     continue
  193.                 continue
  194.             
  195.             break
  196.         if line and quoted_printable:
  197.             while line[-2:] == '=\n':
  198.                 line = line[:-2]
  199.                 newline = ifile.readline()
  200.                 if newline[:len(QUOTE)] == QUOTE:
  201.                     newline = newline[len(QUOTE):]
  202.                 
  203.                 line = line + newline
  204.             line = mime_decode(line)
  205.         
  206.         if line and is_base64 and not pref:
  207.             import base64 as base64
  208.             line = base64.decodestring(line)
  209.         
  210.         if line:
  211.             ofile.write(pref + line)
  212.             continue
  213.         continue
  214.         return None
  215.  
  216.  
  217. def unmimify(infile, outfile, decode_base64 = 0):
  218.     '''Convert quoted-printable parts of a MIME mail message to 8bit.'''
  219.     if type(infile) == type(''):
  220.         ifile = open(infile)
  221.         if type(outfile) == type('') and infile == outfile:
  222.             import os as os
  223.             (d, f) = os.path.split(infile)
  224.             os.rename(infile, os.path.join(d, ',' + f))
  225.         
  226.     else:
  227.         ifile = infile
  228.     if type(outfile) == type(''):
  229.         ofile = open(outfile, 'w')
  230.     else:
  231.         ofile = outfile
  232.     nifile = File(ifile, None)
  233.     unmimify_part(nifile, ofile, decode_base64)
  234.     ofile.flush()
  235.  
  236. mime_char = re.compile('[=\x7f-\xff]')
  237. mime_header_char = re.compile('[=?\x7f-\xff]')
  238.  
  239. def mime_encode(line, header):
  240.     '''Code a single line as quoted-printable.
  241.     If header is set, quote some extra characters.'''
  242.     if header:
  243.         reg = mime_header_char
  244.     else:
  245.         reg = mime_char
  246.     newline = ''
  247.     pos = 0
  248.     if len(line) >= 5 and line[:5] == 'From ':
  249.         newline = ('=%02x' % ord('F')).upper()
  250.         pos = 1
  251.     
  252.     while None:
  253.         res = reg.search(line, pos)
  254.         if res is None:
  255.             break
  256.         
  257.         newline = newline + line[pos:res.start(0)] + ('=%02x' % ord(res.group(0))).upper()
  258.         pos = res.end(0)
  259.         continue
  260.         line = newline + line[pos:]
  261.         newline = ''
  262.         while len(line) >= 75:
  263.             i = 73
  264.             while line[i] == '=' or line[i - 1] == '=':
  265.                 i = i - 1
  266.             i = i + 1
  267.             newline = newline + line[:i] + '=\n'
  268.             line = line[i:]
  269.         return newline + line
  270.  
  271. mime_header = re.compile('([ \t(]|^)([-a-zA-Z0-9_+]*[\x7f-\xff][-a-zA-Z0-9_+\x7f-\xff]*)(?=[ \t)]|\n)')
  272.  
  273. def mime_encode_header(line):
  274.     '''Code a single header line as quoted-printable.'''
  275.     newline = ''
  276.     pos = 0
  277.     while None:
  278.         res = mime_header.search(line, pos)
  279.         if res is None:
  280.             break
  281.         
  282.         newline = '%s%s%s=?%s?Q?%s?=' % (newline, line[pos:res.start(0)], res.group(1), CHARSET, mime_encode(res.group(2), 1))
  283.         pos = res.end(0)
  284.         continue
  285.         return newline + line[pos:]
  286.  
  287. mv = re.compile('^mime-version:', re.I)
  288. cte = re.compile('^content-transfer-encoding:', re.I)
  289. iso_char = re.compile('[\x7f-\xff]')
  290.  
  291. def mimify_part(ifile, ofile, is_mime):
  292.     '''Convert an 8bit part of a MIME mail message to quoted-printable.'''
  293.     has_cte = is_qp = is_base64 = 0
  294.     multipart = None
  295.     must_quote_body = must_quote_header = has_iso_chars = 0
  296.     header = []
  297.     header_end = ''
  298.     message = []
  299.     message_end = ''
  300.     hfile = HeaderFile(ifile)
  301.     while None:
  302.         line = hfile.readline()
  303.         if not line:
  304.             break
  305.         
  306.         if not must_quote_header and iso_char.search(line):
  307.             must_quote_header = 1
  308.         
  309.         if mv.match(line):
  310.             is_mime = 1
  311.         
  312.         if cte.match(line):
  313.             has_cte = 1
  314.             if qp.match(line):
  315.                 is_qp = 1
  316.             elif base64_re.match(line):
  317.                 is_base64 = 1
  318.             
  319.         
  320.         mp_res = mp.match(line)
  321.         if mp_res:
  322.             multipart = '--' + mp_res.group(1)
  323.         
  324.         if he.match(line):
  325.             header_end = line
  326.             break
  327.         
  328.         continue
  329.         while None:
  330.             line = ifile.readline()
  331.             if not line:
  332.                 break
  333.             
  334.             if multipart:
  335.                 if line == multipart + '--\n':
  336.                     message_end = line
  337.                     break
  338.                 
  339.                 if line == multipart + '\n':
  340.                     message_end = line
  341.                     break
  342.                 
  343.             
  344.             if is_base64:
  345.                 message.append(line)
  346.                 continue
  347.             
  348.             if is_qp:
  349.                 while line[-2:] == '=\n':
  350.                     line = line[:-2]
  351.                     newline = ifile.readline()
  352.                     if newline[:len(QUOTE)] == QUOTE:
  353.                         newline = newline[len(QUOTE):]
  354.                     
  355.                     line = line + newline
  356.                 line = mime_decode(line)
  357.             
  358.             if not has_iso_chars:
  359.                 if iso_char.search(line):
  360.                     has_iso_chars = must_quote_body = 1
  361.                 
  362.             
  363.             if not must_quote_body:
  364.                 if len(line) > MAXLEN:
  365.                     must_quote_body = 1
  366.                 
  367.             len(line) > MAXLEN
  368.             continue
  369.             for line in header:
  370.                 if must_quote_header:
  371.                     line = mime_encode_header(line)
  372.                 
  373.                 chrset_res = chrset.match(line)
  374.                 if chrset_res:
  375.                     if has_iso_chars:
  376.                         if chrset_res.group(2).lower() == 'us-ascii':
  377.                             line = '%s%s%s' % (chrset_res.group(1), CHARSET, chrset_res.group(3))
  378.                         
  379.                     else:
  380.                         line = '%sus-ascii%s' % chrset_res.group(1, 3)
  381.                 
  382.                 if has_cte and cte.match(line):
  383.                     line = 'Content-Transfer-Encoding: '
  384.                     if is_base64:
  385.                         line = line + 'base64\n'
  386.                     elif must_quote_body:
  387.                         line = line + 'quoted-printable\n'
  388.                     else:
  389.                         line = line + '7bit\n'
  390.                 
  391.                 ofile.write(line)
  392.             
  393.     if (must_quote_header or must_quote_body) and not is_mime:
  394.         ofile.write('Mime-Version: 1.0\n')
  395.         ofile.write('Content-Type: text/plain; ')
  396.         if has_iso_chars:
  397.             ofile.write('charset="%s"\n' % CHARSET)
  398.         else:
  399.             ofile.write('charset="us-ascii"\n')
  400.     
  401.     if must_quote_body and not has_cte:
  402.         ofile.write('Content-Transfer-Encoding: quoted-printable\n')
  403.     
  404.     ofile.write(header_end)
  405.     for line in message:
  406.         if must_quote_body:
  407.             line = mime_encode(line, 0)
  408.         
  409.         ofile.write(line)
  410.     
  411.     ofile.write(message_end)
  412.     line = message_end
  413.     while multipart:
  414.         if line == multipart + '--\n':
  415.             while None:
  416.                 line = ifile.readline()
  417.                 if not line:
  418.                     return None
  419.                 if must_quote_body:
  420.                     line = mime_encode(line, 0)
  421.                 
  422.                 continue
  423.         line == multipart + '--\n'
  424.         if line == multipart + '\n':
  425.             nifile = File(ifile, multipart)
  426.             mimify_part(nifile, ofile, 1)
  427.             line = nifile.peek
  428.             if not line:
  429.                 break
  430.             
  431.             ofile.write(line)
  432.             continue
  433.         
  434.         while None:
  435.             line = ifile.readline()
  436.             if not line:
  437.                 return None
  438.             if must_quote_body:
  439.                 line = mime_encode(line, 0)
  440.             
  441.             continue
  442.         return None
  443.  
  444.  
  445. def mimify(infile, outfile):
  446.     '''Convert 8bit parts of a MIME mail message to quoted-printable.'''
  447.     if type(infile) == type(''):
  448.         ifile = open(infile)
  449.         if type(outfile) == type('') and infile == outfile:
  450.             import os
  451.             (d, f) = os.path.split(infile)
  452.             os.rename(infile, os.path.join(d, ',' + f))
  453.         
  454.     else:
  455.         ifile = infile
  456.     if type(outfile) == type(''):
  457.         ofile = open(outfile, 'w')
  458.     else:
  459.         ofile = outfile
  460.     nifile = File(ifile, None)
  461.     mimify_part(nifile, ofile, 0)
  462.     ofile.flush()
  463.  
  464. import sys
  465. if (__name__ == '__main__' or len(sys.argv) > 0) and sys.argv[0] == 'mimify':
  466.     import getopt
  467.     usage = 'Usage: mimify [-l len] -[ed] [infile [outfile]]'
  468.     decode_base64 = 0
  469.     (opts, args) = getopt.getopt(sys.argv[1:], 'l:edb')
  470.     if len(args) not in (0, 1, 2):
  471.         print usage
  472.         sys.exit(1)
  473.     
  474.     if ((('-e', '') in opts) == (('-d', '') in opts) or ('-b', '') in opts) and ('-d', '') not in opts:
  475.         print usage
  476.         sys.exit(1)
  477.     
  478.     for o, a in opts:
  479.         if o == '-e':
  480.             encode = mimify
  481.             continue
  482.         None if o == '-d' else None<EXCEPTION MATCH>(ValueError, OverflowError)
  483.         if o == '-b':
  484.             decode_base64 = 1
  485.             continue
  486.     
  487.     if len(args) == 0:
  488.         encode_args = (sys.stdin, sys.stdout)
  489.     elif len(args) == 1:
  490.         encode_args = (args[0], sys.stdout)
  491.     else:
  492.         encode_args = (args[0], args[1])
  493.     if decode_base64:
  494.         encode_args = encode_args + (decode_base64,)
  495.     
  496.     encode(*encode_args)
  497.  
  498.